package cn.edu.pzhu.javaweb.dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import cn.edu.pzhu.javaweb.dao.UserDao;
import cn.edu.pzhu.javaweb.pojo.User;
import cn.edu.pzhu.javaweb.util.DBUtil;

public class UserDaoimp implements UserDao {

	@Override
public int deleteById(String id) {
	Connection con = null;
	PreparedStatement pstm = null;
	try {
		con = DBUtil.getConnection();
		String sql = "delete from user where username=?";
		pstm = con.prepareStatement(sql);
		pstm.setString(1, id);
		return pstm.executeUpdate();
	} catch (SQLException e) {
		e.printStackTrace();
		return 0;
	} finally {
		DBUtil.close(con, pstm);
	}
}

	@Override
public int update(User entity) {
	Connection con = null;
	PreparedStatement pstm = null;
	try {
		con = DBUtil.getConnection();
		String sql;
		if (entity.getStatus() == null) {
			// 只更新密码
			sql = "update user set password=? where username=?";
			pstm = con.prepareStatement(sql);
			pstm.setString(1, entity.getPassword());
			pstm.setString(2, entity.getUsername());
		} else {
			// 同时更新密码和状态
			sql = "update user set password=?, status=? where username=?";
			pstm = con.prepareStatement(sql);
			pstm.setString(1, entity.getPassword());
			pstm.setInt(2, entity.getStatus());
			pstm.setString(3, entity.getUsername());
		}
		return pstm.executeUpdate();
	} catch (SQLException e) {
		e.printStackTrace();
		return 0;
	} finally {
		DBUtil.close(con, pstm);
	}
}

	@Override
	public User selectById(String id) {
		Connection con = null;
	    PreparedStatement pstm = null;
	    ResultSet res = null;
	    User user = null;
	    try {
	        con = DBUtil.getConnection();
	        String sql = "select * from user where username=?";
	        pstm = con.prepareStatement(sql);
	        pstm.setString(1, id);
	        res = pstm.executeQuery();
	        while (res.next()) {
	            user = new User();
	            user.setUsername(res.getString(1));
	            user.setPassword(res.getString(2));
	            user.setStatus(res.getInt(3));
	        }
	        return user;
	    } catch (SQLException e) {
	        e.printStackTrace();
	        return null;
	    } finally {
	        DBUtil.close(con, pstm, res);
	    }
	}
	

	@Override
	public List<User> selectAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int batchInsert(List<User> entities) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int batchDelete(List<String> ids) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int insert(User entity) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int insert(User entity, Connection con) {
		PreparedStatement pstm = null;
	    try {
	        String sql = "insert into user values(?,?,?)";
	        pstm = con.prepareStatement(sql);
	        pstm.setString(1, entity.getUsername());
	        pstm.setString(2, entity.getPassword());
	        pstm.setInt(3, entity.getStatus());
	        int n = pstm.executeUpdate();
	        return n;
	    } catch (SQLException e) {
	        e.printStackTrace();
	        return 0;
	    } finally {
	        DBUtil.close(pstm);
	    }
	}

}
